import time
from selenium.webdriver.common.by import By
from common.Utils import BlogDirver

class BlogLogin:
    driver = BlogDirver.driver
    url = "http://8.137.19.140:9090/blog_login.html"
    driver.get(url)

    driver.implicitly_wait(5)#隐式等待
    def SucLogin(self):
        time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")

        BlogDirver.GetScreenShot()
        # self.driver.back()
    def FailLogin(self):
        time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 正确的账号和错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234567")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(2)
        alert = self.driver.switch_to.alert
        assert (alert.text == "密码错误")
        alert.accept()

        # 错误的用户名和正确的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan1")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(2)
        alert = self.driver.switch_to.alert
        assert (alert.text == "用户不存在")
        alert.accept()

        # 都为空
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(2)
        alert = self.driver.switch_to.alert
        assert (alert.text == "账号或密码不能为空")
        alert.accept()
